<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "student".
 *
 * @property int $id
 * @property string $name
 * @property int $age
 * @property int $sex
 * @property string $phone
 */
class Student extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'student';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['age', 'sex', 'phone'], 'integer'],
            [['name'], 'string', 'max' => 32],
            ['age','match','pattern'=>'/^(?:[1-9]?\d|100)$/'],
            ['phone','match','pattern'=>'/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/','message'=>'手机格式不正确'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'age' => '年龄',
            'sex' => '性别',
            'phone' => '手机号码',
            'create_tm' => '添加时间',
            'update_tm' => '修改时间',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)){
            if ($insert){
                $this->create_tm = time();
                $this->update_tm = time();
            }else{
                $this->update_tm =time();
            }
            return true;
        }else{
            return false;
        }
    }
}
